using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml;
using System.Web;

namespace DeliciousTest
{
    class Delicious
    {
        private System.Net.NetworkCredential credentials;
        private WebRequest request;
        private string username;
        private string password;

        public Delicious()
        {
            //constructor
        }

        public void SetCredentials(string _username, string _password)
        {
            username = _username;
            password = _password;
        }

        //This method has the general logic to setup a connection to the del.icio.us API
        private void SetupConnection(string type, string uriName)
        {
            //create placeholder URI and create a new webrequest for it
            Uri uri = new Uri(uriName);
            request = HttpWebRequest.Create(uri);

            //setup network credentials
            credentials = new System.Net.NetworkCredential(username, password);
            request.Credentials = credentials;

            //set the HTTP method being used
            request.Method = type;
        }

        //create a webresponse to read the response from the del.icio.us API, and do the
        //voodoo to change it into an XML document
        /// <revisionHistory>
        /// <revision author="Moayad Mardini [mardini]" date="26-August-2006">
        /// Add Try-Catch block
        /// </revision>
        /// </revisionHistory>
        private XmlDocument WebResponseToXML()
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            try
            {
                System.Net.WebResponse response = request.GetResponse();
                System.IO.Stream stream = response.GetResponseStream();
                System.IO.StreamReader reader = new System.IO.StreamReader(stream);
                doc.LoadXml(reader.ReadToEnd());
                stream.Close();
                reader.Close();
            }
            catch
            {
              System.Windows.Forms.MessageBox.Show(@"Error, you may wrote a wrong username or a password",@"Error",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
            }
            return doc;
        }

        public XmlDocument GetUserTags()
        {
            SetupConnection("GET","https://api.del.icio.us/v1/tags/get");
            XmlDocument doc = WebResponseToXML();
            return doc;
        }

        public void TagThread(string tag, string desc, string ID)
        {
            //this code will construct the URL to pass to the API
            string URL = "https://api.del.icio.us/v1/posts/add?url=http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=" + ID + "&SiteID=1&description=" + desc;
            if(tag.Contains("MSDNForums"))
                URL += "&tags="+tag;
            else
                URL += "&tags=MSDNForums."+tag;
            //URL = HttpUtility.UrlEncode(URL);
            SetupConnection("GET", URL);
            XmlDocument doc = WebResponseToXML();
        }

        public XmlDocument SearchRecentThreads(string tag)
        {
            string URL = "https://api.del.icio.us/v1/posts/recent?tag=" + tag;
            SetupConnection("GET", URL);
            XmlDocument doc = WebResponseToXML();
            return doc;
        }
        public XmlDocument GetPostsUnderTag(string tag)
        {
            string URL = "https://api.del.icio.us/v1/posts/get?tag=" + tag;
            SetupConnection("GET", URL);
            XmlDocument doc = WebResponseToXML();
            return doc;
        }
        public XmlDocument DeleteBookmark(string _URL)
        {
            string URL = "https://api.del.icio.us/v1/posts/delete?url=" + _URL;
            SetupConnection("GET", URL);
            XmlDocument doc = WebResponseToXML();
            return doc;
        }
    }
}
